package seller

import (
	"dhfshop/models/seller"
	"time"
	"dhfshop/controllers/base"
	"dhfshop/models/consts"
	"github.com/astaxie/beego/validation"
)

// 
type CashReqController struct {
	base.BaseController
}

func (this *CashReqController) List() {
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	l, cnt, err := seller.GetAllCashReqs(query,fields,order_by,offset,limit)
	if err != nil {
	    this.ServeError(err.Error())
	    return
	}
		r := struct {
		    TotalCount int64          `json:"total_count"`
	        CashReqs   []*seller.CashReq `json:"cash_reqs"`
	    }{TotalCount: cnt, CashReqs: l}
	    this.Data["json"] = r
	this.ServeJSON()
}

func (this *CashReqController) Post() {
	var v seller.CashReq
	valid := validation.Validation{}
	this.ParseForm(&v)
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	v.PayTime, _ = time.Parse(consts.DATETIME_FORMAT,v.PayTimeStr)
	if _, err := seller.AddCashReq(&v); err != nil {
             this.ServeError(err.Error())
		return
    }
	rsp := struct {
		CashReqId   int64        `json:"cash_req_id"`
	    Created   time.Time  `json:"created"`
	}{CashReqId: v.CashReqId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *CashReqController) Put() {
	var v seller.CashReq
	//var items []seller.OrderItem
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.CashReqId, "cash_req_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	var updateFields []string
    if err := seller.UpdateCashReqById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		    CashReqId   int64         `json:"cash_req_id"`
	        Modified  time.Time   `json:"modified"`
	    }{CashReqId:v.CashReqId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *CashReqController) StatusUpdate() {
	v := struct {
	    CashReqId    int64    `form:"cash_req_id" valid:"Required"`
		Event        string   `form:"event" valid:"Required"`
		Remark       string   `form:"remark"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	//event.TradeBus.Publish(v.Event,message,&result)
	rsp := struct {
		    CashReqId   int64         `json:"cash_req_id"`
	        Modified  time.Time   `json:"modified"`
	    }{CashReqId:v.CashReqId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *CashReqController) Delete() {
    //companyId := this.Ctx.Input.GetData("curCompanyId").(int)
    //curUserId := this.Ctx.Input.GetData("curUserId").(int)
	cashReqId, _ := this.GetInt("cash_req_id")
	if cashReqId == 0 {
		this.ServeError("cash_req_id is null")
		return 
	}
	
	if err := seller.DeleteCashReq(int64(cashReqId)); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		CashReqId  int64       `json:"cash_req_id"`
	    Modified   time.Time   `json:"modified"`
	}{CashReqId:int64(cashReqId), Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}
